#include "hplcqinfinepumpparsesystempacket.h"
#include "common/hplcqinfinepfcid.h"
#include "common/hplcqinfinepacketformat.h"
#include <QDebug>

#include "modulehplccmdid.h"

namespace hplc
{
namespace qinfine
{

HplcQinfinePumpParseSystemPacket::HplcQinfinePumpParseSystemPacket(HplcQinfinePumpProtocolData &data, hplc::PumpParam &param, QObject *parent)
    : QObject (parent)
    , m_d (&data)
    , m_param(&param)
{

}

void HplcQinfinePumpParseSystemPacket::parseSet(quint8 pfc, char *data)
{
    switch (pfc)
    {
    case PFC_PES_WORKMODE:
    {
        m_param->systemParam.workMode = data[0];
        emit setWorkMode(m_param->systemParam.workMode);

        QVariant var;
        var.setValue(m_param->systemParam.workMode);
        emit parseCompleted(hplc::PumpSystemCmdID_SetWorkMode, var);
    }
        break;
    case PFC_PES_FLOWCALIB_DATA_CMD:
        if (data[0])
        {
//            qDebug() << "clear ratecalibtable";
            m_param->systemParam.rateCalibTable.clear();
        }
        else
        {
            // 保存

//            qDebug() << "ratecalibratetable = ";
//            foreach (auto var, m_param->systemParam.rateCalibTable)
//            {
//                qDebug() << var.rpm << var.rate;
//            }

            emit setFlowCalibData(m_param->systemParam.rateCalibTable);

            QVariant var;
            var.setValue(m_param->systemParam.rateCalibTable);
            emit parseCompleted(hplc::PumpSystemCmdID_SetFlowCalibData, var);
        }
        break;
    case PFC_PES_FLOWCALIB_DATA:
    {
        hplc::PumpRateCalibData cell;
        quint32 temp = PTR2U32(data);
        cell.rpm = *(float*)(&temp);

        if (m_param->systemParam.rateCalibTable.size())
        {
            if (qFuzzyCompare(cell.rpm, m_param->systemParam.rateCalibTable.last().rpm))
                break;
        }

        temp = PTR2U32(data + 4);
        cell.rate = *(float*)(&temp);
        m_param->systemParam.rateCalibTable << cell;
    }
        break;
    case PFC_PES_FLOWCALIB_LOAD_FLOW:
    {
        quint32 temp = PTR2U32(data);
        float flow = *(float*)(&temp);
        m_param->systemParam.loadCalibParam.rate = flow;
        emit setFlowCalibLoadFlow(flow);

        QVariant var;
        var.setValue(m_param->systemParam.loadCalibParam.rate);
        emit parseCompleted(hplc::PumpSystemCmdID_SetFlowCalibLoadFlow, var);
    }
        break;
    case PFC_PES_FLOWCALIB_LOAD_REALFLOW:
    {
        quint32 temp = PTR2U32(data);
        float flow = *(float*)(&temp);
        m_param->systemParam.loadCalibParam.realRate = flow;
        emit setFlowCalibLoadRealFlow(flow);

        QVariant var;
        var.setValue(m_param->systemParam.loadCalibParam.realRate);
        emit parseCompleted(hplc::PumpSystemCmdID_SetFlowCalibLoadRealFlow, var);
    }
        break;
    case PFC_PES_FLOWCALIB_LOAD_PRESS:
    {
        quint32 temp = PTR2U32(data);
        float press = *(float*)(&temp);
        m_param->systemParam.loadCalibParam.pressure = press;
        emit setFlowCalibLoadPressure(press);

        QVariant var;
        var.setValue(m_param->systemParam.loadCalibParam.pressure);
        emit parseCompleted(hplc::PumpSystemCmdID_SetFlowCalibLoadPress, var);
    }
        break;
    case PFC_PES_PRESSCALIB_DATA_CMD:
        if (data[0])
        {
            m_param->systemParam.pressureCalibTable.clear();
        }
        else
        {
            // 保存

            emit setPressureCalibData(m_param->systemParam.pressureCalibTable);

            QVariant var;
            var.setValue(m_param->systemParam.pressureCalibTable);
            emit parseCompleted(hplc::PumpSystemCmdID_SetPressCalibData, var);
        }
        break;
    case PFC_PES_PRESSCALIB_DATA:
    {
        hplc::PumpPressureCalibData cell;
        quint32 temp = PTR2U32(data);
        cell.adc = *(float*)(&temp);

        if (m_param->systemParam.pressureCalibTable.size())
        {
            if (qFuzzyCompare(cell.adc, m_param->systemParam.pressureCalibTable.last().adc))
                break;
        }

        temp = PTR2U32(data + 4);
        cell.pressure = *(float*)(&temp);
        m_param->systemParam.pressureCalibTable << cell;
    }
        break;
    case PFC_PES_PULSECOMPEN_DATA_CMD:
        if (data[0])
        {
            m_param->systemParam.pulseCompenCalibTable.clear();
        }
        else
        {
            // 保存

            emit setPulseCompenCalibData(m_param->systemParam.pulseCompenCalibTable);

            QVariant var;
            var.setValue(m_param->systemParam.pulseCompenCalibTable);
            emit parseCompleted(hplc::PumpSystemCmdID_SetPulseCompenCalibData, var);
        }
        break;
    case PFC_PES_PULSECOMPEN_DATA:
    {
        hplc::PumpPulseCompenCalibData cell;
        quint32 temp = PTR2U32(data);
        cell.position = *(float*)(&temp);

        if (m_param->systemParam.pulseCompenCalibTable.size())
        {
            if (cell.position == m_param->systemParam.pulseCompenCalibTable.last().position)
                break;
        }

        temp = PTR2U32(data + 4);
        cell.factor = *(float*)(&temp);
        m_param->systemParam.pulseCompenCalibTable << cell;
    }
        break;
    case PFC_PES_PULSECOMPEN_DEBUG_CMD:
        break;
    case PFC_PES_PULSECOMPEN_GEAR:
        break;
    case PFC_PES_PULSECOMPEN_GEAR_POS:
        {
            qint32 pos = (int32_t)PTR2U32(data);
            emit setPulseCompenGearPos(pos);

            QVariant var;
            var.setValue(pos);
            emit parseCompleted(hplc::PumpSystemCmdID_SetPulseCompenGearPos, var);
        }
        break;
    case PFC_PES_STEPMOTOR_IRUN:
    {
        m_param->systemParam.stepMotorIRun = data[0];
        emit setMotorIRun(data[0]);

        QVariant var;
        var.setValue(m_param->systemParam.stepMotorIRun);
        emit parseCompleted(hplc::PumpSystemCmdID_SetStepMotorIRun, var);
    }
        break;
    case PFC_PES_MOTORCURRENT_DATA_CMD:
        if (data[0])
        {
            m_param->systemParam.stepMotorCurrentCalibTable.clear();
        }
        else
        {
            // 保存

            emit setMotorCurrentCalibData(m_param->systemParam.stepMotorCurrentCalibTable);

            QVariant var;
            var.setValue(m_param->systemParam.stepMotorCurrentCalibTable);
            emit parseCompleted(hplc::PumpSystemCmdID_SetMotorCurrentCalibData, var);
        }
        break;
    case PFC_PES_MOTORCURRENT_DATA:
    {
        hplc::PumpStepMotorCurrentCalibData cell;
        quint32 temp = PTR2U32(data);
        cell.rpm = *(float*)(&temp);

        if (m_param->systemParam.stepMotorCurrentCalibTable.size())
        {
            if (qFuzzyCompare(cell.rpm, m_param->systemParam.stepMotorCurrentCalibTable.last().rpm))
                break;
        }

        cell.current = data[4];
        m_param->systemParam.stepMotorCurrentCalibTable << cell;
    }
        break;
    case PFC_PES_MOTOR_CONTROLMODE:
        break;
    case PFC_PES_MOTOR_DIVFREQ:
        break;
    case PFC_PES_MOTOR_FSPR:
        break;
    case PFC_PES_MOTOR_CURRENT_STANDBY:
        break;
    default:
        break;
    }
}

void HplcQinfinePumpParseSystemPacket::parseGet(quint8 pfc, char *data)
{
    switch (pfc)
    {
    case PFC_PES_WORKMODE:
        break;
    case PFC_PES_FLOWCALIB_DATA_CMD:
        break;
    case PFC_PES_FLOWCALIB_DATA:
        break;
    case PFC_PES_FLOWCALIB_LOAD_FLOW:
        break;
    case PFC_PES_FLOWCALIB_LOAD_REALFLOW:
        break;
    case PFC_PES_FLOWCALIB_LOAD_PRESS:
        break;
    case PFC_PES_PRESSCALIB_DATA_CMD:
        break;
    case PFC_PES_PRESSCALIB_DATA:
        break;
    case PFC_PES_PULSECOMPEN_DATA_CMD:
        break;
    case PFC_PES_PULSECOMPEN_DATA:
        break;
    case PFC_PES_PULSECOMPEN_DEBUG_CMD:
        break;
    case PFC_PES_PULSECOMPEN_GEAR:
        break;
    case PFC_PES_PULSECOMPEN_GEAR_POS:
        break;
    case PFC_PES_STEPMOTOR_IRUN:
        break;
    case PFC_PES_MOTORCURRENT_DATA_CMD:
        break;
    case PFC_PES_MOTORCURRENT_DATA:
        break;
    case PFC_PES_MOTOR_CONTROLMODE:
        break;
    case PFC_PES_MOTOR_DIVFREQ:
        break;
    case PFC_PES_MOTOR_FSPR:
        break;
    case PFC_PES_MOTOR_CURRENT_STANDBY:
        break;
    default:
        break;
    }
}

} // qinfine
} // hplc
